import md5 from 'js-md5'
const base64 = require('js-base64').Base64;

import { notification, Modal, message } from 'ant-design-vue'

import {BoxType, Placement} from 'assets/js/Constant'

// 给指定对象赋值
export const set2Form = (target, source) =>  {
  for (const key in target) {
    target[key] = source[key];
  }
};

export const clearForm = form => {
  for (const key in form) {
    if(Array.isArray(form[key])) {
      form[key] = []
    } else if((typeof form[key]) == 'number') {
      form[key] = 0;
    } else {
      form[key] = ""
    }
  }
}

// MD加密
export const hex_md5 = data => {
  return md5(data);
}

// base64编码
export const hex_encode = data => {
  return base64.encode(data);
}

// base64解码
export const hex_decode = data => {
  return base64.decode(data);
}

// 全局提醒消息
export const showMess = (msg = "OK", type = BoxType.SUCCESS, onClose = () => {}) => {
  message[type]({
    content: msg,
    duration: 3,
    onClose
  });
}

// 通知提醒消息
export const showNoti = (msg = 'OK', type = BoxType.SUCCESS, placement = Placement.TOP_RIGHT) => {
  notification[type]({
    message: "提示消息",
    description: msg,
    duration: 3,
    placement: placement
  });
}

// 确定框
export const confirm = (fn, msg = '确定执行该操作?', cancelFn = () => {}) => {
  Modal.confirm({
    title: '确认信息',
    cancelText: "关闭",
    okText: "确认",
    content: msg,
    onOk: fn,
    onCancel: cancelFn
  })
}

/**
 * 验证输出是否为手机号
 * @param {*} msg 
 * @returns 匹配 true
 */
export const isTel = msg => {
  return (/^[1](([3][0-9])|([4][5,7,9])|([5][^4,6,9])|([6][6])|([7][3,5,6,7,8])|([8][0-9])|([9][8,9]))[0-9]{8}$/.test(msg));
}

/**
 * 验证是否为邮箱
 * @param {*} msg
 * @returns  匹配 true
 */
export const isEmail = msg => {
  return (/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(msg))
}

export const showImg = key => {
  return process.env.VUE_APP_IMG_URL + "/" + key;
}